package day11.Generics;

/*
 * Author: JacksonCoder
 * FileName: GenericsDemo
 * Version: 1.0.0
 * Date: 2025/4/18
 */

import java.util.Arrays;
import java.util.List;

// 泛型类
class Box<T> {
    private T value;
    public void set(T value){
        this.value = value;
    }

    public T get(){
        return value;
    }
}

// 泛型接口
interface Printer<T>{
    void print(T value);
}

class StringPrinter implements Printer<String>{

    @Override
    public void print(String value) {
        System.out.println("Value:" + value);
    }
}

public class GenericsDemo {

    public static void main(String[] args) {
        // 使用
        Box<String> box = new Box<>();
        box.set("hello java");
        System.out.println(box.get());
        System.out.println("===============");
        printArray(new String[]{"Java","Python","GoLang"});
        System.out.println("===============");
        printList(Arrays.asList("hello","nihao","nice","good","pass"));

        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        List<Integer> scores = Arrays.asList(90, 85, 100);
        System.out.println("===============");
        printElements(names);    // 打印字符串列表
        System.out.println("===============");
        printElements(scores);   // 打印整数列表
    }


    // 泛型方法
    public static <T> void printArray(T[] array){
        for(T element : array){
            System.out.println(element);
        }
    }

    // 通配符： ？ 的使用
    public static void printList(List<?> list){
        for(Object obj : list){
            System.out.println(obj);
        }
    }

    // 泛型方法：打印集合中的所有元素
    public static <T> void printElements(List<T> list) {
        for (T item : list) {
            System.out.println("元素: " + item);
        }
    }


}
